package org.workplicity.android.cia.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.ExecutionException;

import org.workplicity.android.cia.ui.MainActivity;
import org.workplicity.elog.entry.ElogUser;
import org.workplicity.elog.entry.ElogUser.Function;
import org.workplicity.entry.Entry;
import org.workplicity.entry.User;
import org.workplicity.task.InsertTask;
import org.workplicity.task.NetResult;
import org.workplicity.task.NetTask;
import org.workplicity.task.NetTicket;
import org.workplicity.util.Helper;
import org.workplicity.util.Logger;
import org.workplicity.util.Resources;
import org.workplicity.worklet.WorkletContext;

import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;

import android.os.AsyncTask;
import android.widget.Toast;

public class AndroidHelper extends Helper {

        static Boolean loginStatus = false;

        public AndroidHelper() {
                
        }
        
     
        
        /**
     * Inserts an entry into the repository. If the entry id == -1, a new
     * entry is inserted. If id != -1, the method attempts to update the entry
     * of that id, if it is found. Assumes a valid ticket is in the IWC and
     * writes a ticket into the IWC.
     * @param entry Entry
     * @param repos Repository common name
     * @param context WOrklet context
     * @return True if insert successful, false otherwise.
     */
    public static boolean insert(Entry entry, String repos, WorkletContext context) {
        try {
            Logger.log("insert started for " + entry.getClass().getName() + " id=" + entry.getId());

            InsertTask insertTask = new InsertTask();

//            WorkletContext context = GroundsWorklet.getInstance().getContext();

            NetTicket ticket = (NetTicket) context.take(Resources.TAG_TICKET);

            if (ticket == null) {
                Logger.log("internal error: insert failed ticket is null");
                return false;
            }

            // Set the updating user
            User me = (User) context.read(Resources.TAG_USER);

            Integer myId = new Integer(me.getId());

            if (entry.getId() == -1) {
                entry.setCreateUserId(myId);
            }

            entry.setUpdateUserId(myId);

            // Insert / update the entry in the repository
            // Note: this will be an insert if entry id is -1 and
            // an update otherwise
            NetResult result = insertTask.submit(repos, entry, ticket);
            result.check("insert failed");

            Logger.log("insert status = " + result.getStatus());


            ticket = result.getTicket();
            if (ticket != null) {
                context.publish(Resources.TAG_TICKET, ticket);
            }

            // Process the results
            ArrayList<Integer> reply = (ArrayList<Integer>) result.getResult();

            if (reply != null && !reply.isEmpty()) {
                Logger.log("insert/update successful ticket = " + result.getTicket().getNumber());
                Integer id = (Integer) reply.get(0);
                Logger.log("entry id = " + id);

                // Update the entry id for the entry as we may have to
                // put the entry on a work slate later -- and the entry
                // id is required.
                entry.setId(id);

                Logger.log("insert successful for entry id=" + id);
            } else {
                Logger.log("insert reply failed");
            }

            return true;

        } catch (Exception e) {
            Logger.log("exception insert failed: " + e.toString());
        } catch (Error e) {
            Logger.log("error insert failed: " + e.toString());
        }

        return false;
    }

        
        
        public static Boolean getLoginStatus() {
                return loginStatus;
        }

        public static void setLoginStatus(Boolean loginStatus) {
                AndroidHelper.loginStatus = loginStatus;
        }
        

        
}